local hx__zhoutianzheng = fk.CreateSkill{
  name = "hx__zhoutianzheng",

  tags = {Skill.Compulsory},
}

hx__zhoutianzheng:addEffect("active",{
  name = "hx__zhoutianzheng",
  prompt = "#hx__zhoutianzheng_active",
  can_use = function (self, player)
    return not player:isKongcheng() and #player:getPile("@hx__zhoutianzheng__zheng") < 20
  end,
  min_card_num = 1,
  card_filter = function (self, player, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) ~= Player.Equip and (#selected + #player:getPile("@hx__zhoutianzheng__zheng")) < 20
  end,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use =function (self, room, effect)
    local player = effect.from
    room:broadcastPlaySound("./packages/huixiangkill/audio/skill/hx__zhoutianzheng__active")
      local cards = effect.cards
      room:moveCardTo(cards,Player.Special,player,fk.ReasonPut,hx__zhoutianzheng.name,"@hx__zhoutianzheng__zheng",false,player)
  end,
})

hx__zhoutianzheng:addEffect(fk.TurnEnd,{
  name = "#hx__zhoutianzheng__trigger",
  priority = 1.1,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__zhoutianzheng.name) and #player:getPile("@hx__zhoutianzheng__zheng") > 0 and target and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {"hx__zhoutianzheng_trred","hx__zhoutianzheng_trblack"}
    local choices = {}
    local cards = player:getPile("@hx__zhoutianzheng__zheng")
    local rednum = table.filter(cards,function (c) return Fk:getCardById(c).color == Card.Red end)
    local blacknum = table.filter(cards,function (c) return Fk:getCardById(c).color == Card.Black end)
    if #rednum > 0 then
      table.insert(choices,"hx__zhoutianzheng_trred")
    end
    if #blacknum > 0 then
      table.insert(choices,"hx__zhoutianzheng_trblack")
    end
    if #choices > 0 then
      local choice = room:askToChoice(player,{choices = choices , skill_name = hx__zhoutianzheng.name, all_choices = all_choices , cancelable = false})
      if choice == "hx__zhoutianzheng_trred" then
         room:setPlayerMark(player,"hx__zhoutianzheng__num",#rednum)
         local suc,dat = room:askToUseActiveSkill(player,{ skill_name = "#hx__zhoutianzheng__active" , cancelable = false, prompt = "#hx__zhoutianzheng__chooosenum"})
          if suc and dat then
            local realnum = dat.interaction
            room:setPlayerMark(player,"hx__zhoutianzheng__num",0)
            room:recover{
              who = player,
              num = realnum,
              recoverBy = player,
              skillName = self.name,
            }
            local discardnum = math.min(realnum,#table.filter(player.player_cards[Player.Hand],function (c)
              return Fk:getCardById(c).color == Card.Red
            end))
            room:askToDiscard(player,{ min_num = discardnum,max_num = discardnum, skill_name = hx__zhoutianzheng.name, cancelable = false, include_equip = false, pattern = ".|.|heart,diamond" })
        end
      else
          room:setPlayerMark(player,"hx__zhoutianzheng__num",#blacknum)
         local suc,dat = room:askToUseActiveSkill(player,{ skill_name = "#hx__zhoutianzheng__active" , cancelable = false, prompt = "#hx__zhoutianzheng__chooosenum"})
        if suc and dat then
          local realnum = dat.interaction
          room:setPlayerMark(player,"hx__zhoutianzheng__num",0)
          local targets = room:askToChoosePlayers(player,{targets = room.alive_players,min_num = 1, max_num = 999,skill_name = self.name, cancelable = false , prompt = "#hx__zhoutianzheng_choosetar"})
          for _, v in ipairs(targets) do
            room:damage{
              to = v,
              damage = realnum,
              from = player,
              skillName = hx__zhoutianzheng.name,
            }
            local discardnum = math.min(realnum,#table.filter(v.player_cards[Player.Hand],function (c)
            return Fk:getCardById(c).color == Card.Black
          end))
          room:askToDiscard(v,{ min_num = discardnum,max_num = discardnum, skill_name = hx__zhoutianzheng.name, cancelable = false, include_equip = false, pattern = ".|.|club,spade" })
          end
        end
      end
    end
  end,
})

hx__zhoutianzheng:addEffect("filter",{
  name = "#hx__zhoutianzheng_fliter",
  handly_cards = function (self, player)
    if player:hasSkill(hx__zhoutianzheng.name) and #player:getPile("@hx__zhoutianzheng__zheng") > 0 then
      return player:getPile("@hx__zhoutianzheng__zheng")
    end
  end,
})

hx__zhoutianzheng:addEffect(fk.AfterCardsMove,{
  name = "#hx__zhoutianzheng_dicard",
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(hx__zhoutianzheng.name) then
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and move.moveReason == fk.ReasonDiscard and move.from.phase == Player.Discard then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) then
              return #player:getPile("@hx__zhoutianzheng__zheng") < 20
            end
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
        if move.from and move.from ~= player and move.moveReason == fk.ReasonDiscard and move.from.phase == Player.Discard then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) then
               if #player:getPile("@hx__zhoutianzheng__zheng") < 20 and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                  player.room:broadcastPlaySound("./packages/huixiangkill/audio/skill/hx__zhoutianzheng__active")
                  player.room:moveCardTo(info.cardId,Player.Special,player,fk.ReasonPut,hx__zhoutianzheng.name,"@hx__zhoutianzheng__zheng",false,player)
               end
            end
          end
        end
      end
  end,
})

hx__zhoutianzheng:addEffect("targetmod",{
  bypass_distances = function (self, player, skill, card, to)
    if player:hasSkill(hx__zhoutianzheng.name) and #player:getPile("@hx__zhoutianzheng__zheng") > 0 then
      return Fk:currentRoom():getCardArea(card) == player.Special and table.contains(player:getPile("@hx__zhoutianzheng__zheng"),card.id)
    end
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    if player:hasSkill(hx__zhoutianzheng.name) and #player:getPile("@hx__zhoutianzheng__zheng") > 0 then
      return Fk:currentRoom():getCardArea(card) == player.Special and table.contains(player:getPile("@hx__zhoutianzheng__zheng"),card.id)
    end
  end,
})

return hx__zhoutianzheng